#include  "iotv30_config.h"
#include "mconfig.h"
#include "stdio.h"


// #include "dev_attr.h"

 #include "iotv30_app.h"

#include "iot_message.pb.h"

#include  "system.h"

#include "test_mode.h"
#include "simple_event.h"

#include "dev_lamp.h"



lamp_attr_t lamp_attr={
    .warning.bit={
        .flooding=0,
        .leak_current=1,
        .open_load=0,
        .over_load=0,
    },
    .threshold={//default value, do not delete
        .warning_config.bit={
            .under_voltage = 1,
            .over_voltage = 1,
            .over_load = 1,
            .open_load = 1,
            .short_circuit = 1,
            .start_fail = 1,
            .under_temperature = 1,
            .over_temperature = 1,
            .leak_current = 1,
            .flooding  = 1,
        },
        .under_voltage_threshold = 1650,//0.1v
        .over_voltage_threshold = 2750,//0.1v
        .over_load_threshold = 1000,//1w
        .leak_current_threshold = 30,//1ma
        .over_temperature_threshold = 95,
        .under_temperature_threshold = -20,
    },
};


/**
 * @brief 
 * 
 * @param out 
 */
void lamp_attr_get(lamp_attr_t* out)
{
    //lock_acquire
    memcpy(out, &lamp_attr, sizeof(lamp_attr_t));
    //lock_release
}

/**
 * @brief 
 * 
 */
void lamp_param_init(void)
{
    snprintf(lamp_attr.info.soft_ver, sizeof(lamp_attr.info.soft_ver),"%s sv%04X %s",SDK_VER_STR,app_get_ver(),__DATE__);
    snprintf(lamp_attr.info.hard_ver, sizeof(lamp_attr.info.hard_ver),"%s",HARDWARE_STR);
    snprintf(lamp_attr.info.production_info, sizeof(lamp_attr.info.production_info),"%s","2024-08-p01");
}

/**
 * @brief 
 * 
 * @param attr_id 
 * @param attr_out 
 */
void lamp_get_attr(uint16_t attr_id, IotModel* attr_out)
{
    attr_out->iot_id = attr_id;   //?????
    
    switch(attr_id)
    {
    case 0x0001:
        attr_out->value = lamp_attr.is_open[0];
        break;
    
    case 0x0002:
        attr_out->value = lamp_attr.is_open[1];
        break;

    case 0x0021:
        attr_out->value = lamp_attr.brightness[0];
        break;

    case 0x0022:
        attr_out->value = lamp_attr.brightness[1];
        break;

    case 0x0031:
        attr_out->value = lamp_attr.temperature;
        break;
        
    case 0x0400:
        attr_out->value = lamp_attr.elec.f_elec[eUU]*10;//0.1V
        break;

    case 0x0401:
        attr_out->value = lamp_attr.elec.f_elec[eUU]*10;//0.1V
        break;

    case 0x0403:
        attr_out->value = lamp_attr.elec.f_elec[eII_1]*100;//0.01A
        break;

    case 0x0404:
        attr_out->value = lamp_attr.elec.f_elec[eII_2]*100;//0.01A
        break;

    case 0x0407:
        attr_out->value = lamp_attr.elec.f_elec[eleak];//1mA
        break;

    case 0x0408:
        attr_out->value = lamp_attr.elec.f_elec[eFQ]*10;//0.1Hz
        break;

    case 0x040C:
        attr_out->value = lamp_attr.elec.f_elec[ePP_1];//1W
        break;

    case 0x040D:
        attr_out->value = lamp_attr.elec.f_elec[ePP_2];//1W
        break;

    case 0x040F:
        attr_out->value = lamp_attr.elec.f_elec[ePF_1]*100;
        break;
    
    case 0x0410:
        attr_out->value = lamp_attr.elec.f_elec[ePF_2]*100;
        break;

    case 0x0412:
        attr_out->value = lamp_attr.elec.f_elec[eEN_1]*100;//0.01kWh
        break;

    case 0x0413:
        attr_out->value = lamp_attr.elec.f_elec[eEN_2]*100;//0.01kWh
        break;

    case 0x0480:
        attr_out->value = lamp_attr.warning.data;
        break;

    case 0x0481:
        attr_out->value = lamp_attr.threshold.warning_config.data;
        break;
    
    case 0x0482:
        attr_out->value = lamp_attr.threshold.under_voltage_threshold;
        break;
    
    case 0x0483:
        attr_out->value = lamp_attr.threshold.over_voltage_threshold;
        break;

    case 0x0484:
        attr_out->value = lamp_attr.threshold.over_load_threshold;
        break;

    case 0x0485:
        attr_out->value = lamp_attr.threshold.leak_current_threshold;
        break;

    case 0x0486:
        attr_out->value = lamp_attr.threshold.over_temperature_threshold;
        break;

    case 0x0487:
        attr_out->value = (uint8_t)lamp_attr.threshold.under_temperature_threshold;
        break;

    case 0x0800:
        attr_out->value = CTK_PID;
        break;

    case 0x0801:
        attr_out->values.size = strlen(lamp_attr.info.soft_ver);
        strncpy((char*)attr_out->values.bytes,lamp_attr.info.soft_ver,32);
        break;

    case 0x0802:
        attr_out->values.size = strlen(lamp_attr.info.hard_ver);
        strncpy((char*)attr_out->values.bytes,lamp_attr.info.hard_ver,32);
        break;

    case 0x0803:
        attr_out->values.size = strlen(lamp_attr.info.production_info);
        strncpy((char*)attr_out->values.bytes,lamp_attr.info.production_info,32);
        break;
    }
}

/**
 * @brief 
 * 
 * @param attr_in 
 * @return uint8_t 
 */
uint8_t lamp_set_attr(IotModel* attr_in)
{
    printf_log(lv0,"set addr %04X, value = %d\n", attr_in->iot_id, attr_in->value);

    switch(attr_in->iot_id)
    {
    case 0x0001:
        lamp_attr.is_open[0] = attr_in->value ? 1 : 0;
        if(lamp_attr.is_open[0] == 0){
            //添加关灯代码
        }
        break;

    case 0x0002:
        lamp_attr.is_open[1] = attr_in->value ? 1 : 0;
        if(lamp_attr.is_open[1] == 0){
            //添加关灯代码
        }
        break;

    case 0x0021:
        lamp_attr.brightness[0] = attr_in->value;
        if(lamp_attr.is_open[0] == 1){
            //添加开灯代码
        }
        break;

    case 0x0022:
        lamp_attr.brightness[1] = attr_in->value;
        if(lamp_attr.is_open[1] == 1){
            //添加开灯代码
        }
        break;

    case 0x0480:
        if(attr_in->value == 0){//清除告警
            lamp_attr.warning.data = 0;
        }
        break;

    case 0x0481:
        lamp_attr.threshold.warning_config.data = attr_in->value;
        simple_event_send(event_save_threshold, 100);//写任何一个参数都触发保存
        break;
    
    case 0x0482:
        lamp_attr.threshold.under_voltage_threshold = attr_in->value;
        simple_event_send(event_save_threshold, 100);
        break;
    
    case 0x0483:
        lamp_attr.threshold.over_voltage_threshold = attr_in->value;
        simple_event_send(event_save_threshold, 100);
        break;

    case 0x0484:
        lamp_attr.threshold.over_load_threshold = attr_in->value;
        simple_event_send(event_save_threshold, 100);
        break;

    case 0x0485:
        lamp_attr.threshold.leak_current_threshold = attr_in->value;
        simple_event_send(event_save_threshold, 100);
        break;

    case 0x0486:
        lamp_attr.threshold.over_temperature_threshold = attr_in->value;
        simple_event_send(event_save_threshold, 100);
        break;

    case 0x0487:
        lamp_attr.threshold.under_temperature_threshold = (int8_t)attr_in->value;
        simple_event_send(event_save_threshold, 100);
        break;

    default: 
        return 0;
    }
    return 1;
}





